package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.result.Result;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

import com.hgxd.service.ITbUserService;
import com.hgxd.entity.pojo.TbUser;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author wtf
 * @since 2023-11-20
 */
@Api(tags = "工人表")
@RestController
@RequestMapping("/api/tbUser")
@CrossOrigin
public class TbUserController {
    @Resource(name ="TbUserServiceImpl")
    private ITbUserService tbUserService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "无需传入id")
    public Result<Boolean> insert(@RequestBody TbUser tbUser) {
        return Result.ok(tbUserService.save(tbUser));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "必须传入id")
    public Result<Boolean> update(@RequestBody TbUser tbUser) {
        return Result.ok(tbUserService.updateById(tbUser));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "必须传入id")
    public Result<Boolean> delete(@PathVariable Integer id) {
        return Result.ok(tbUserService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "传入ids")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(tbUserService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "无需传参")
    public Result<List<TbUser>> findAll() {
        return Result.ok(tbUserService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个", notes = "传入id")
    public Result<TbUser> findOne(@PathVariable Integer id) {
        return Result.ok(tbUserService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "传入对应所需参数")
    public Result<Page<TbUser>> findPage(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize) {
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(tbUserService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

